// pages/perfectData/perfectData.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var list = [];
Page({
    data: {
        type:'',
        region: ['', ''],
        consultant_index: 0,
        consultants: [],
        consultant: [],  
        fromstu:'',   //来源
        userSex: 1,
        stu_id: 0,
        phone:'',
        tel: '',
        changetel: false,
        idcard: '',
        changecard: false,
        user: {},
        truename: '',
        note: '',
        multiIndex: [0, 0],
        multiArray: CONFIG.MULTIARRAY,
        objectMultiArray: CONFIG.OBJECTMULTIARRAY,
        showfalse1:false,
        submitfalsephone:false,
        customstuts: [],
        customindex:-1,
    },

    
    // 点击选择客户状态
    clickcustom(e){
        var that = this;
        // console.log(e);
        var customindex = e.target.dataset.index;
        that.setData({
            customindex: customindex
        })
    },

    closeclick(){
        var that = this;
        that.setData({
            submitfalsephone: false
        })
    },

    
    // 拨打电话
    phonetal(){
        var that = this;
        that.setData({
            submitfalsephone:true
        })
        
    },

    messnoneendphone(){
        var that = this;
        that.setData({
            submitfalsephone: false
        })
        var phone = that.data.phone;
        wx.setClipboardData({
            data: phone,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none',
                    duration: 1500
                })
            },
            fail: function (res) {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                    duration: 1500
                })
            },
        })
    },

    
    subjectbutphone(){
        var that = this;
        that.setData({
            submitfalsephone: false
        })
        var phone = that.data.phone;
        wx.makePhoneCall({
            phoneNumber: phone //仅为示例，并非真实的电话号码
        })
    },


    // 地址
    bindMultiPickerChange: function (e) {
        // console.log(this.data.multiArray[0][this.data.multiIndex[0]]);
        // vm = this;
        this.setData({
        "multiIndex[0]": e.detail.value[0],
        "multiIndex[1]": e.detail.value[1]
        })
        this.setData({
        region: [this.data.multiArray[0][this.data.multiIndex[0]], this.data.multiArray[1][this.data.multiIndex[1]]]
        })
    },

    bindMultiPickerColumnChange: function (e) {
        // vm = this;
        var that = this;
        switch (e.detail.column) {
        case 0:
            list = []
            for (var i = 0; i < that.data.objectMultiArray.length; i++) {
            if (that.data.objectMultiArray[i].parid == that.data.objectMultiArray[e.detail.value].regid) {
                list.push(that.data.objectMultiArray[i].regname)
            }
            }
            that.setData({
            "multiArray[1]": list,
            "multiIndex[0]": e.detail.value,
            "multiIndex[1]": 0
            })

        }
    },

    bindRegionChange: function (e) {
        // console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
        region: e.detail.value
        })
    },

    bindConsultantChange: function (e) {
        var consultant_index = e.detail.value;
        var consultant = this.data.consultants[consultant_index];
        this.setData({
        consultant: consultant,
        consultant_index: consultant_index
        })
    },

    userSexChange: function (e) {
        // console.log(8989)
        this.setData({
        userSex: parseInt(e.detail.value)
        })
    },

    nameblur: function (e) {
        if (e.detail.value != '') {
        this.setData({
            truename: e.detail.value
        })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
        onLoad: function (options) {
        // wx.showLoading({
        //   title: '加载中~',
        //   mask: true
        // })
            var type = options.type;
            this.setData({
                stu_id: options.stu_id,
                phone: options.phone,
                type: options.type,
            });
            if (type == 1 || type == '1'){
                wx.setNavigationBarTitle({
                    title: '完善资料'
                })
            }else{
                wx.setNavigationBarTitle({
                    title: '客户详情'
                })
            }

            this.getCrmUser(options.phone);
        },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let that = this;
        //获取课程顾问信息
        wx.request({
        url: CONFIG.API_HOST + 'crm/public/getConsultants',
        method: 'GET',
        success: function (res) {
            // console.log(res);
            wx.hideLoading();
            that.setData({
                consultant: res.data.consultants[0],
                consultants: res.data.consultants
            });
        },
        fail: function (res) {
        }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getCrmUser(this.data.phone);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getCrmUser(this.data.phone);
        wx.stopPullDownRefresh();
    },
    // 用户点击右上角分享

    onShareAppMessage: function () {
        return {
        title: CONFIG.SHARETITLE,
        path: CONFIG.PATHLOGIN
        }
    },

    /**
     * 查询客户信息
     */
    getCrmUser: function (phone) {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        let that = this;
        //获取绑定信息 
        wx.request({
            // url: CONFIG.API_HOST + 'crm/user/getUserInfo', 
            url: CONFIG.API_HOST + 'crm/user/getUserInfoNewCon', 
        data: {
            phone: phone,
            consultant_id: app.globalData.admin_id
        },
        success: function (res) {
            // console.log(res);
            if (res.data.data.data){
                var data = res.data.data;
                that.setData({
                    user: data.data,
                    userSex: data.data.sex,
                    region: data.region,
                    note: data.data.note,
                    customstuts: data.tag,
                    customindex: -1,
                });
            }else{
                that.setData({
                    user: {},
                    userSex: 1,
                    region: ['', ''],
                    note: ''
                });
            }
            // console.log(that.data.note);
            wx.hideLoading();
        },
        fail: function (res) {
            // console.log(res);
            // that.getCrmUser(stu_id);
            wx.hideLoading();
            wx.showModal({
            title: CONFIG.MODEL_TITLE,
            showCancel: false,
            content: CONFIG.REQUEST_FAIL,
            confirmText: '点击重试',
            success: function (res) {
                wx.navigateBack();
            }
            })
        }
        })
    },

    bindFormSubmit: function (e) {
        var e1 = e.detail.value;
        var consultant_id = this.data.user.consultant_id;
        if (!consultant_id) {
          consultant_id = this.data.consultant.admin_id;
        }
        var nae = e1.truename;
        var name = nae.replace(/\s+/g, "");
        if (name == '') {
            wx.showToast({
                title: '请填写姓名',
                icon: 'none',
                duration: 1500
            })
        } else if (!(/^[\u2E80-\u9FFF]+$/.test(name))) {
            wx.showToast({
                title: '姓名只能为汉字',
                icon: 'none',
                duration: 1500
            })
        } else if (name.length > 5) {
            wx.showToast({
                title: '姓名最多为5个字',
                icon: 'none',
                duration: 1500
            })
        } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(e1.tel)) || e1.tel == '') {
            wx.showToast({
                title: '请填写正确手机号',
                icon: 'none',
                duration: 1500
            })
        }else {
            var type = this.data.type;
            
            if (type == 1 || type == '1') {
                if (this.data.region[0] == ''){
                   var regin = '';
                    var add = '';
                }else{
                    var regin = this.data.region[0];
                    var add = this.data.region[0] + ',' + this.data.region[1]
                }
                if (this.data.userSex){
                    var sex = this.data.userSex;
                }else{
                    var sex = ''; 
                }
                if (this.data.customindex!=-1){
                    var tag_status = Number(this.data.customindex) + 1
                }else{
                    var tag_status = '';
                }
                var params = {
                    name: e.detail.value.truename,
                    phone: e1.tel,
                    oldPhone: this.data.phone,
                    sex: sex,
                    region: regin,
                    address: add,
                    note: e1.textarea,
                    tag_status: tag_status,
                    consultant_id: app.globalData.admin_id
                }
                this.submit(params);
            }else{
                if(this.data.region[0] == '') {
                    wx.showToast({
                        title: '请选择地址',
                        icon: 'none',
                        duration: 1500
                    })
                }else{
                    if (this.data.customindex != -1) {
                        var tag_status = Number(this.data.customindex)+1
                    } else {
                        var tag_status = '';
                    }
                    var params = {
                        name: e.detail.value.truename,
                        phone: e1.tel,
                        oldPhone:this.data.phone,
                        sex: this.data.userSex,
                        region: this.data.region[0],
                        address: this.data.region[0] + ',' + this.data.region[1],
                        note: e1.textarea,
                        tag_status: tag_status,
                        consultant_id: app.globalData.admin_id
                    }
                    this.submit(params);
                }
            }
        
        }

    },
    sureclick1(){
        this.setData({
            showfalse1: false,
        })
        wx.navigateBack()
    },
    submit(params){
        var that = this;
        wx.showLoading({
            title: '提交中~',
            mask: true
        })
      // console.log(params);
        wx.request({
            url: CONFIG.API_HOST + 'crm/User/updateUserInfoConsNew',
            data: params,
            header: {
                //设置参数内容类型为x-www-form-urlencoded
                'content-type': 'application/x-www-form-urlencoded',
                'Accept': 'application/json'
            },
            method: 'POST',
            success: function (res) {
            //   console.log(res);
                wx.hideLoading();
                if (res.data.status == 1001 || res.data.status == '1001'){
                    that.setData({
                        showfalse1:true,
                    })
                }else{
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1500
                    })
                }
            },
            fail: function (res) {
                wx.hideLoading();
                wx.showModal({
                    title: CONFIG.MODEL_TITLE,
                    showCancel: false,
                    content: CONFIG.REQUEST_FAIL,
                    confirmText: '点击重试',
                    success: function (res) {
                        wx.navigateBack();
                    }
                })
            }
        })
  }
})